package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.UserBean;
import connection.DBConnection;

public class UserPaging {
	int countrow = 0;

	public List<UserBean> UserList(int offset,int noOfRecords)throws SQLException {
		Statement stmt = null;
		Connection currentCon = null;
	    ResultSet rs = null;
		List<UserBean> list = new ArrayList<UserBean>();

		
		String search =
            "SELECT SQL_CALC_FOUND_ROWS * FROM user LIMIT "+offset+","+noOfRecords;		
		try {
		currentCon = DBConnection.getConnection();
        stmt = currentCon.createStatement();
      //  System.out.println("before resultset");
        rs = stmt.executeQuery(search);
       // System.out.println("after resultset");
        while (rs.next()){
    	    UserBean userbean = new UserBean();
        	userbean.setUserName(rs.getString("user"));
        	list.add(userbean);
        }
        rs.close();
        
        rs = stmt.executeQuery("SELECT FOUND_ROWS()");
        if(rs.next())
            this.countrow = rs.getInt(1);
		
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                }
                rs = null;
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                }
                stmt = null;
            }

            if (currentCon != null) {
                try {
                    currentCon.close();
                } catch (Exception e) {
                }

                currentCon = null;
            }
        }
        return list;
    }
	public int getNoOfRecords() {
        return countrow;
    }
}
